﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ContractorTimesheet.DataModel;

namespace ContractorTimesheet.Models
{
    public class UserEditorModel
    {
        public UserEditorModel()
        {
            UsersInfo = new List<UserInfo>();

            ResetNewUser();
        }

        public void ResetNewUser()
        {
            NewFirstname = string.Empty;
            NewLastname = string.Empty;
            NewEmail = string.Empty;
            NewPO = string.Empty;
            NewBadgeNo = string.Empty;
            NewIsActive = true;
        }

        public List<UserInfo> UsersInfo { get; set; }

        public int NewId { get; set; }

        public string NewFirstname { get; set; }

        public string NewLastname { get; set; }

        public string NewEmail { get; set; }

        public string NewPO { get; set; }

        public string NewBadgeNo { get; set; }

        public bool NewIsActive { get; set; }

        public UserEditorModel SortByName()
        {
            UserEditorModel model = new UserEditorModel();
            model.NewFirstname = NewFirstname;
            model.NewLastname = NewLastname;
            model.NewEmail = NewEmail;
            model.NewPO = NewPO;
            model.NewBadgeNo = NewBadgeNo;
            model.NewIsActive = NewIsActive;
            model.UsersInfo = UsersInfo.OrderBy(m => m.Firstname).ThenBy(m => m.Lastname).ToList();

            return model;
        }

    }

    public class UserInfo
    {
        public UserInfo() { }

        public UserInfo(User user) 
        {
            Id = user.Id;
            Firstname = user.Firstname;
            Lastname = user.Lastname;
            Email = user.Email;
            PO = user.PO;
            BadgeNo = user.BadgeNo;
            IsActive = user.IsActive;
        }

        public int Id { get; set; }

        public string Firstname { get; set; }

        public string Lastname { get; set; }

        public string Email { get; set; }

        public string PO { get; set; }

        public string BadgeNo { get; set; }

        public bool IsActive { get; set; }

        public static IList<UserInfo> CreateUsersInfo(IList<User> users)
        {
            List<UserInfo> usersInfo = new List<UserInfo>();

            foreach (User user in users)
                usersInfo.Add(new UserInfo(user));

            return usersInfo;
        }

    }
}